
package com.shop.cloud.mall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.mall.admin.mapper.ActiveBackgroundMapper;
import com.shop.cloud.mall.admin.service.ActiveBackgroundRelationService;
import com.shop.cloud.mall.admin.service.ActiveBackgroundService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.ActiveBackground;
import com.shop.cloud.mall.common.entity.ActiveBackgroundRelation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动背景
 *
 * @author
 * @date 2023-03-22 17:01:21
 */
@Service
@AllArgsConstructor
public class ActiveBackgroundServiceImpl extends ServiceImpl<ActiveBackgroundMapper, ActiveBackground> implements ActiveBackgroundService {

	private final ActiveBackgroundRelationService activeBackgroundRelationService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BACKGROUND, allEntries = true)
	public boolean save(ActiveBackground entity) {
		super.save(entity);
		if(StrUtil.isNotBlank(entity.getSuitType()) &&
				!MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){//是否指定关联
			doRelation(entity);
		}
		return Boolean.TRUE;
	}

	void doRelation(ActiveBackground entity){
		List<ActiveBackgroundRelation> listActiveBackgroundRelation = entity.getListActiveBackgroundRelation().stream()
				.map(activeBackgroundRelation -> {
					activeBackgroundRelation.setActiveBackgroundId(entity.getId());
					return activeBackgroundRelation;
				}).collect(Collectors.toList());
		activeBackgroundRelationService.saveBatch(listActiveBackgroundRelation);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Caching(evict={
			@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BACKGROUND, allEntries = true),
			@CacheEvict(value = CacheConstants.MALL_GOODS_SPU, allEntries = true)
	})
	public boolean updateById1(ActiveBackground entity) {
		super.updateById(entity);
		if(entity.getListActiveBackgroundRelation() != null || MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){
			//先删除关联
			activeBackgroundRelationService.remove(Wrappers.<ActiveBackgroundRelation>lambdaQuery()
					.eq(ActiveBackgroundRelation::getActiveBackgroundId,entity.getId()));
			if(StrUtil.isNotBlank(entity.getSuitType()) &&
					!MallConstants.ACTIVE_SUIT_TYPE_1.equals(entity.getSuitType())){//是否指定关联
				doRelation(entity);
			}
		}
		return Boolean.TRUE;
	}

	@Override
	public ActiveBackground getById2(Serializable id) {
		return baseMapper.selectById2(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Caching(evict={
			@CacheEvict(value = CacheConstants.MALL_ENABLE_ACTIVE_BACKGROUND, allEntries = true),
			@CacheEvict(value = CacheConstants.MALL_GOODS_SPU, allEntries = true)
	})
	public boolean removeById(Serializable id) {
		//先删除关联
		activeBackgroundRelationService.remove(Wrappers.<ActiveBackgroundRelation>lambdaQuery()
				.eq(ActiveBackgroundRelation::getActiveBackgroundId,id));
		return super.removeById(id);
	}
}
